<template>
  <div class="registered-list-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <div class="spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 错误状态 -->
    <div v-else-if="error" class="error-state">
      <div class="error-icon">⚠️</div>
      <p>{{ error }}</p>
      <button @click="$emit('refresh')" class="retry-btn">重试</button>
    </div>

    <!-- 空状态 -->
    <div v-else-if="events.length === 0" class="empty-state">
      <div class="empty-icon">📋</div>
      <p>您还没有报名任何比赛</p>
      <p class="empty-hint">快去发现有趣的比赛并报名参加吧！</p>
    </div>

    <!-- 报名记录列表 -->
    <div v-else class="registered-events">
      <div class="list-header">
        <h3>我的报名记录 <span class="count-badge">{{ events.length }}</span></h3>
      </div>

      <div
        v-for="event in events"
        :key="getEventId(event)"
        class="registered-event-card"
      >
        <div class="event-main-info">
          <div class="event-header">
            <h4 class="event-name">{{ event.eventName }}</h4>
            <span class="status-badge registered">已报名</span>
          </div>
          <div class="event-details">
            <div class="detail-item">
              <span class="label">比赛时间：</span>
              <span class="value">{{ formatDate(event.eventTime) }}</span>
            </div>
            <div class="detail-item">
              <span class="label">比赛地点：</span>
              <span class="value">{{ event.eventLocation }}</span>
            </div>
            <div class="detail-item">
              <span class="label">比赛类别：</span>
              <span class="value">{{ event.eventCategory }}</span>
            </div>
          </div>
        </div>

        <div class="event-actions">
          <button
            @click="$emit('view-detail', getEventId(event))"
            class="btn btn-view"
          >
            查看详情
          </button>
          <button
            @click="$emit('cancel-registration', getEventId(event))"
            class="btn btn-cancel"
          >
            取消报名
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
interface Event {
  eventId?: number;
  eventID?: string;
  eventName: string;
  eventTime: string;
  eventLocation: string;
  eventCategory: string;
  eventDesc?: string;
  price?: number;
  maxParticipants?: number;
  participantCount?: number;
}

interface Props {
  events: Event[];
  loading: boolean;
  error: string;
}

// 🎯 修复：移除未使用的 props 和 emit 变量
defineProps<Props>();
defineEmits<{
  (e: 'refresh'): void;
  (e: 'view-detail', eventId: number): void;
  (e: 'cancel-registration', eventId: number): void;
}>();

// 获取事件ID
const getEventId = (event: Event): number => {
  return Number(event.eventID || event.eventId || 0);
};

// 格式化日期
const formatDate = (dateString: string): string => {
  if (!dateString) return '暂无时间';
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};
</script>

<style scoped>
/* 样式保持不变 */
.registered-list-container {
  padding: 20px 0;
}

/* 加载状态 */
.loading-state {
  text-align: center;
  padding: 60px 0;
}

.spinner {
  width: 60px;
  height: 60px;
  margin: 0 auto 20px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #31B310;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-state p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

/* 错误状态 */
.error-state {
  text-align: center;
  padding: 60px 0;
  color: #dc3545;
}

.error-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.error-state p {
  margin-bottom: 20px;
  font-size: 16px;
}

.retry-btn {
  padding: 10px 24px;
  background: linear-gradient(135deg, #31B310, #2a990e);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.retry-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 80px 0;
  color: #6c757d;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
  opacity: 0.5;
}

.empty-hint {
  margin-top: 10px;
  font-size: 14px;
  opacity: 0.7;
}

/* 列表头部 */
.list-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f0f2f5;
}

.list-header h3 {
  color: #2c3e50;
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
}

.count-badge {
  background: linear-gradient(135deg, #31B310, #2a990e);
  color: white;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

/* 报名记录卡片 */
.registered-events {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.registered-event-card {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 24px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.registered-event-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  border-color: #31B310;
}

.event-main-info {
  flex: 1;
}

.event-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  gap: 16px;
}

.event-name {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
}

.status-badge {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.status-badge.registered {
  background: linear-gradient(135deg, #31B310, #2a990e);
  color: white;
}

.event-details {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 12px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.label {
  color: #6c757d;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}

.value {
  color: #495057;
  font-size: 14px;
}

/* 操作按钮 */
.event-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 120px;
  margin-left: 20px;
}

.btn {
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.btn-view {
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
}

.btn-view:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.btn-cancel {
  background: linear-gradient(135deg, #ff4d4f, #d9363e);
  color: white;
}

.btn-cancel:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .registered-event-card {
    flex-direction: column;
    gap: 20px;
  }

  .event-actions {
    flex-direction: row;
    width: 100%;
    justify-content: flex-end;
    margin-left: 0;
  }

  .event-details {
    grid-template-columns: 1fr;
  }

  .event-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
}
</style>
